<?php
namespace app\crm\controller;
use think\Loader;

class Role extends CrmController
{
    /**
     * 角色列表界面
     * @return [type] [description]
     */
    public function roleList()
    {
		//分配增删改的权限
		$menuname = "Role";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		// 模板输出
        return $this->fetch('roleList');
    }
	
    /**
     * 获取角色列表资料
     * @return [type] [description]
     */
	public function roleDataList()
    {
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        
        $arrRole = Loader::model('Role')->getRoleData($page,$limit);

		echo json_encode($arrRole);
	}
	
    /**
     * 调用model添加角色
     * @return [type] [description]
     */
	public function insertRole()
    {
		   
        $data = array(
            'r_name' => $_REQUEST['r_name'],
            'description' => $_REQUEST['description'],
            'interface' => $_REQUEST['interface']
        );
        Loader::model('Role')->insertRole($data);
	}

    /**
     * 调用model更新角色
     * @return [type] [description]
     */
    public function updateRole()
    {   
        $data = array(
            'r_name' => $_REQUEST['r_name'],
            'description' => $_REQUEST['description'],
            'interface' => $_REQUEST['interface']
        );
        Loader::model('Role')->updateRole($_REQUEST["r_id"],$data);
    }

    /**
     * 删除角色
     * @return [type] [description]
     */
    function delRole()
    {
        $r_id = $_REQUEST["r_id"];
        Loader::model('Role')->delRole($r_id);
    }
	
    /**
     * 输出添加角色模板
     */
    public function addRole()
    {	
		// 模板输出
        return $this->fetch('addRole');
    }
	
    /**
     * 输出编辑角色模板
     */
    public function editRole()
    {
        $r_id = $_GET["r_id"];
        $role_data = Loader::model('Role')->getRoleArr($r_id);
        $this->assign("roleData",$role_data);
        // 模板输出
        return $this->fetch('editRole');
    }

    /**
     * 输出添加权限树
     */
    public function roleTree()
    {
        //角色的相关权限
        $r_id=$_GET['r_id'];
        if($r_id){
            $arrRole = Loader::model('Role')->getRoleArr($r_id);
            if($arrRole){
                $arrPriv = json_decode( $arrRole['action_list'],true );//权限转换为数组
            }else{
                $this->error('要分配的权限的角色不存在！');
            }
        }else{
            $this->error('权限项不存在！');
        }

        $roleName = $arrRole['r_name'];
        $this->assign("roleName",$roleName);
        
        $strJSON = Loader::model('Role')->roleTree($arrPriv);

        $this->assign('id',$r_id);
        $this->assign("strJSON",$strJSON);
        return $this->fetch('roleTree');
    }


    /**
     * 更新权限
     * @return [type] [description]
     */
    public function savePriv(){
        $r_id = $_POST['r_id'];
        $json = $_POST['json'];
        Loader::model('Role')->savePriv($r_id,$json);
    }

}